package org.dyndns.opendemogroup.optimizer.operations;

import java.util.Random;

import org.dyndns.opendemogroup.optimizer.IOperation;
import org.dyndns.opendemogroup.optimizer.IOptimizationProblem;
import org.dyndns.opendemogroup.optimizer.Member;
import org.dyndns.opendemogroup.optimizer.Pair;

/**
 * The second simplest form of crossover, based on OnePointCrossover
 */
public class TwoPointCrossover implements IOperation
{

	public void perform ( Random randomSource, IOptimizationProblem problem,
			Member parentOne, Member parentTwo, Member childOne, Member childTwo )
	{
		final int memberSize = parentOne.getMemberSize ( );
		final int lastIndex = memberSize - 1;
		Pair<Integer, Integer> points =
			OrderPreservingOperationBase.pickPoints ( randomSource, memberSize );
		final int a = points.first, b = points.second;

		// { perform crossover
		childOne.copyFrom ( parentOne, 0, a );
		childOne.copyFrom ( parentTwo, a + 1, b );
		childOne.copyFrom ( parentOne, b + 1, lastIndex );

		childTwo.copyFrom ( parentTwo, 0, a );
		childTwo.copyFrom ( parentOne, a + 1, b );
		childTwo.copyFrom ( parentTwo, b + 1, lastIndex );
		// }
	}

}
